package com.neusoft.demo3.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.demo3.bean.Cart;
import com.neusoft.demo3.config.AjaxResult;
import com.neusoft.demo3.mapper.CartMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/xiaomi/cart")
public class CartController {
    @Autowired
    private CartMapper cartMapper;
    @PostMapping ("save")
    public AjaxResult save(@RequestBody Cart cart){
        int rows=cartMapper.insert(cart);
        if(rows>0){
            return AjaxResult.success();
        }
        else {
            return  AjaxResult.exception("添加购物车失败");
        }
    }

    @GetMapping("list")
    public AjaxResult list(@RequestParam Integer userId) {
        try {
            QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId); // 按用户ID查询购物车
            List<Cart> cartList = cartMapper.selectList(queryWrapper);
            return AjaxResult.success(cartList); // 返回购物车列表
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.exception("查询购物车失败");
        }
    }
}
